package com.richesup.common.oss.controller;

import com.richesup.common.oss.common.CodeMessage;
import com.richesup.common.oss.common.OSSException;
import com.richesup.common.oss.common.ResultEntity;
import com.richesup.common.oss.common.SystemConfig;
import com.richesup.common.oss.dao.AppMapper;
import com.richesup.common.oss.entity.App;
import com.richesup.common.oss.entity.Bucket;
import com.richesup.common.oss.service.IApp;
import com.richesup.common.oss.service.IBucket;
import com.richesup.common.oss.service.IFileUpload;
import com.richesup.common.oss.service.impl.FileUpload;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;

/**
 * Created by 13120 on 2018-6-21.
 */
@RestController
public class ImageUploadController {
    private final static Logger logger= LogManager.getLogger(ImageUploadController.class);

    @Autowired
    IFileUpload fileUpload;

    @Autowired
    SystemConfig systemconfig;

    @Autowired
    IApp appService;

    @Autowired
    IBucket bucketService;

    /**
     *
     * 功能描述:上传单个图片
     *
     * @param: [文件流, app_id, app_secret, bucket_id]
     * @return: com.richesup.common.oss.common.ResultEntity
     * @auther: yuhong
     * @date: 2018-6-26 21:53
     */
    @PostMapping("/ImageUpload")
    public ResultEntity ImageUpload(@RequestParam("file") MultipartFile file,@RequestParam("app_id")String app_id,
                                    @RequestParam("app_secret")String app_secret,@RequestParam("bucket_id")String bucket_id){


        //验证APP信息
        App app= appService.verifyApp(app_id,app_secret);

        //验证bucket信息
        Bucket bucket=bucketService.verifyBucket(app_id,bucket_id);

        //存储路径：BucketPath加上日期、时间目录："BucketID/yyyy/MM/dd"，例：sinocvs/2018/06/27/
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat objSimpleDateFormat=new SimpleDateFormat(new StringBuilder().append("yyyy").append(File.separator).append("MM").append(File.separator).append("dd").append(File.separator).toString());
        String strPath=bucket.getBucket_path()+ File.separator+objSimpleDateFormat.format(calendar.getTime());

        //上传图片
        String strFileName=fileUpload.Upload(file,systemconfig.getSavePath()+ File.separator+strPath);

        //尺寸转换（未做）

        //数据库存储
        String strFileID=fileUpload.SaveData(app_id,strPath,strFileName);

        //存不同尺寸的图

        HashMap hm=new HashMap();
        hm.put("FileID",strFileID);
        hm.put("FilePath",strPath);
        hm.put("FileName",strFileName);
        return  ResultEntity.success(hm);




    }




}
